package migrate

import (
	"sale-admin/config/mysql"
	"sale-admin/internal/models"
)

// 同步正式环境的索引
func (MigrateFunc) Script32() error {
	db := mysql.NewDB()
	// ma_activity_data
	if !db.Migrator().HasIndex(&models.MaActivityData{}, "idx_actid") {
		db.Migrator().CreateIndex(&models.MaActivityData{}, "idx_actid")
	}
	if !db.Migrator().HasIndex(&models.MaActivityData{}, "idx_redep") {
		db.Migrator().CreateIndex(&models.MaActivityData{}, "idx_redep")
	}
	if db.Migrator().HasIndex(&models.MaActivityData{}, "created_at") {
		db.Migrator().DropIndex(&models.MaActivityData{}, "created_at")
	}
	if !db.Migrator().HasIndex(&models.MaActivityData{}, "Name") {
		db.Migrator().CreateIndex(&models.MaActivityData{}, "Name")
	}
	if !db.Migrator().HasIndex(&models.MaActivityData{}, "SalesID") {
		db.Migrator().CreateIndex(&models.MaActivityData{}, "SalesID")
	}
	if !db.Migrator().HasIndex(&models.MaActivityData{}, "Position") {
		db.Migrator().CreateIndex(&models.MaActivityData{}, "Position")
	}
	if !db.Migrator().HasIndex(&models.MaActivityData{}, "FileName") {
		db.Migrator().CreateIndex(&models.MaActivityData{}, "FileName")
	}

	// ma_ai_service_face_count
	if db.Migrator().HasIndex(&models.MaAIServiceFaceCount{}, "StartTime") {
		db.Migrator().DropIndex(&models.MaAIServiceFaceCount{}, "StartTime")
	}
	if db.Migrator().HasIndex(&models.MaAIServiceFaceCount{}, "idx_starttime_createddata") {
		db.Migrator().DropIndex(&models.MaAIServiceFaceCount{}, "idx_starttime_createddata")
	}
	if !db.Migrator().HasIndex(&models.MaAIServiceFaceCount{}, "SalesID") {
		db.Migrator().CreateIndex(&models.MaAIServiceFaceCount{}, "SalesID")
	}
	if !db.Migrator().HasIndex(&models.MaAIServiceFaceCount{}, "RobotName") {
		db.Migrator().CreateIndex(&models.MaAIServiceFaceCount{}, "RobotName")
	}
	if !db.Migrator().HasIndex(&models.MaAIServiceFaceCount{}, "AddTime") {
		db.Migrator().CreateIndex(&models.MaAIServiceFaceCount{}, "AddTime")
	}
	if !db.Migrator().HasIndex(&models.MaAIServiceFaceCount{}, "idx_merchant_id") {
		db.Migrator().CreateIndex(&models.MaAIServiceFaceCount{}, "idx_merchant_id")
	}
	if !db.Migrator().HasIndex(&models.MaAIServiceFaceCount{}, "idx_starttime_facecount0_facecount1_facecount2") {
		db.Migrator().CreateIndex(&models.MaAIServiceFaceCount{}, "idx_starttime_facecount0_facecount1_facecount2")
	}
	if !db.Migrator().HasIndex(&models.MaAIServiceFaceCount{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaAIServiceFaceCount{}, "created_at")
	}

	//ma_goods_activity
	if !db.Migrator().HasIndex(&models.MaGoodsActivity{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaGoodsActivity{}, "created_at")
	}

	// ma_goods_activity_detail
	if !db.Migrator().HasIndex(&models.MaGoodsActivityDetail{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaGoodsActivityDetail{}, "created_at")
	}

	// ma_goods_class
	if !db.Migrator().HasIndex(&models.MaGoodsClass{}, "ClassID") {
		db.Migrator().CreateIndex(&models.MaGoodsClass{}, "ClassID")
	}
	if !db.Migrator().HasIndex(&models.MaGoodsClass{}, "ma_goods_ibfk_1") {
		db.Migrator().CreateIndex(&models.MaGoodsClass{}, "ma_goods_ibfk_1")
	}
	if !db.Migrator().HasIndex(&models.MaGoodsClass{}, "ClassName1") {
		db.Migrator().CreateIndex(&models.MaGoodsClass{}, "ClassName1")
	}
	if !db.Migrator().HasIndex(&models.MaGoodsClass{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaGoodsClass{}, "created_at")
	}

	// ma_goods_click
	if db.Migrator().HasIndex(&models.MaGoodsClick{}, "SalesID") {
		db.Migrator().DropIndex(&models.MaGoodsClick{}, "SalesID")
	}
	if db.Migrator().HasIndex(&models.MaGoodsClick{}, "AddTime") {
		db.Migrator().DropIndex(&models.MaGoodsClick{}, "AddTime")
	}
	if !db.Migrator().HasIndex(&models.MaGoodsClick{}, "GoodsID") {
		db.Migrator().CreateIndex(&models.MaGoodsClick{}, "GoodsID")
	}
	if !db.Migrator().HasIndex(&models.MaGoodsClick{}, "idx_ID_StartTime") {
		db.Migrator().CreateIndex(&models.MaGoodsClick{}, "idx_ID_StartTime")
	}
	if !db.Migrator().HasIndex(&models.MaGoodsClick{}, "idx_merchantid_addtime_starttime") {
		db.Migrator().CreateIndex(&models.MaGoodsClick{}, "idx_merchantid_addtime_starttime")
	}
	if !db.Migrator().HasIndex(&models.MaGoodsClick{}, "end_time") {
		db.Migrator().CreateIndex(&models.MaGoodsClick{}, "end_time")
	}
	if !db.Migrator().HasIndex(&models.MaGoodsClick{}, "idx_smc") {
		db.Migrator().CreateIndex(&models.MaGoodsClick{}, "idx_smc")
	}
	if !db.Migrator().HasIndex(&models.MaGoodsClick{}, "idx_sgse") {
		db.Migrator().CreateIndex(&models.MaGoodsClick{}, "idx_sgse")
	}

	// ma_goods_info
	if !db.Migrator().HasIndex(&models.MaGoodsInfo{}, "GoodsID") {
		db.Migrator().CreateIndex(&models.MaGoodsInfo{}, "GoodsID")
	}
	if !db.Migrator().HasIndex(&models.MaGoodsInfo{}, "ma_goods_info_ibfk_2") {
		db.Migrator().CreateIndex(&models.MaGoodsInfo{}, "ma_goods_info_ibfk_2")
	}

	// ma_goods_multi
	if db.Migrator().HasIndex(&models.MaGoodsMulti{}, "created_at") {
		db.Migrator().DropIndex(&models.MaGoodsMulti{}, "created_at")
	}

	// ma_merchant_config
	db.Exec("alter table ma_merchant_config drop primary key")
	db.Exec("alter table ma_merchant_config add primary key(`id`)")
	if !db.Migrator().HasIndex(&models.MaMerchantConfig{}, "ma_user_shop_info_ibfk_2") {
		db.Migrator().CreateIndex(&models.MaMerchantConfig{}, "ma_user_shop_info_ibfk_2")
	}

	// ma_order
	if db.Migrator().HasIndex(&models.MaOrder{}, "AddTime") {
		db.Migrator().DropIndex(&models.MaOrder{}, "AddTime")
	}
	if db.Migrator().HasIndex(&models.MaOrder{}, "created_at") {
		db.Migrator().DropIndex(&models.MaOrder{}, "created_at")
	}
	if db.Migrator().HasIndex(&models.MaOrder{}, "updated_at") {
		db.Migrator().DropIndex(&models.MaOrder{}, "updated_at")
	}
	if db.Migrator().HasIndex(&models.MaOrder{}, "idx_end") {
		db.Migrator().DropIndex(&models.MaOrder{}, "idx_end")
	}
	if db.Migrator().HasIndex(&models.MaOrder{}, "idx_child") {
		db.Migrator().DropIndex(&models.MaOrder{}, "idx_child")
	}
	if db.Migrator().HasIndex(&models.MaOrder{}, "idx_EndOutTradeNo") {
		db.Migrator().DropIndex(&models.MaOrder{}, "idx_EndOutTradeNo")
	}
	if !db.Migrator().HasIndex(&models.MaOrder{}, "OrderID") {
		db.Migrator().CreateIndex(&models.MaOrder{}, "OrderID")
	}
	if !db.Migrator().HasIndex(&models.MaOrder{}, "SalesID") {
		db.Migrator().CreateIndex(&models.MaOrder{}, "SalesID")
	}
	if !db.Migrator().HasIndex(&models.MaOrder{}, "PaymentID") {
		db.Migrator().CreateIndex(&models.MaOrder{}, "PaymentID")
	}
	if !db.Migrator().HasIndex(&models.MaOrder{}, "ReFunTradeNo") {
		db.Migrator().CreateIndex(&models.MaOrder{}, "ReFunTradeNo")
	}
	if !db.Migrator().HasIndex(&models.MaOrder{}, "idx_discount_amount") {
		db.Migrator().CreateIndex(&models.MaOrder{}, "idx_discount_amount")
	}
	if !db.Migrator().HasIndex(&models.MaOrder{}, "idx_aomdp") {
		db.Migrator().CreateIndex(&models.MaOrder{}, "idx_aomdp")
	}
	if !db.Migrator().HasIndex(&models.MaOrder{}, "idx_child_out_trade_no") {
		db.Migrator().CreateIndex(&models.MaOrder{}, "idx_child_out_trade_no")
	}
	if !db.Migrator().HasIndex(&models.MaOrder{}, "idx_end_out_trade_no") {
		db.Migrator().CreateIndex(&models.MaOrder{}, "idx_end_out_trade_no")
	}
	// ma_order_details
	if !db.Migrator().HasIndex(&models.MaOrderDetails{}, "SalesID") {
		db.Migrator().CreateIndex(&models.MaOrderDetails{}, "SalesID")
	}
	if !db.Migrator().HasIndex(&models.MaOrderDetails{}, "AddTime") {
		db.Migrator().CreateIndex(&models.MaOrderDetails{}, "AddTime")
	}

	// ma_order_points
	db.Exec("alter table ma_order_points drop primary key")
	db.Exec("alter table ma_order_points drop index `id`")
	db.Exec("alter table ma_order_points add primary key(`id`)")
	if db.Migrator().HasIndex(&models.MaOrderPoints{}, "updated_at") {
		db.Migrator().DropIndex(&models.MaOrderPoints{}, "updated_at")
	}
	if !db.Migrator().HasIndex(&models.MaOrderPoints{}, "SalesID") {
		db.Migrator().CreateIndex(&models.MaOrderPoints{}, "SalesID")
	}
	if !db.Migrator().HasIndex(&models.MaOrderPoints{}, "OutTradeNo") {
		db.Migrator().CreateIndex(&models.MaOrderPoints{}, "OutTradeNo")
	}
	if !db.Migrator().HasIndex(&models.MaOrderPoints{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaOrderPoints{}, "created_at")
	}

	// ma_order_refund
	if !db.Migrator().HasIndex(&models.MaOrderRefund{}, "SalesID") {
		db.Migrator().CreateIndex(&models.MaOrderRefund{}, "SalesID")
	}
	if !db.Migrator().HasIndex(&models.MaOrderRefund{}, "OutTradeNo") {
		db.Migrator().CreateIndex(&models.MaOrderRefund{}, "OutTradeNo")
	}
	if !db.Migrator().HasIndex(&models.MaOrderRefund{}, "AddTime") {
		db.Migrator().CreateIndex(&models.MaOrderRefund{}, "AddTime")
	}
	if !db.Migrator().HasIndex(&models.MaOrderRefund{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaOrderRefund{}, "created_at")
	}

	// ma_payment_alipay
	// if !db.Migrator().HasIndex(&models.MaPaymentAlipay{}, "BindID") {
	// 	db.Migrator().CreateIndex(&models.MaPaymentAlipay{}, "BindID")
	// }
	// if !db.Migrator().HasIndex(&models.MaPaymentAlipay{}, "AliPayAppID") {
	// 	db.Migrator().CreateIndex(&models.MaPaymentAlipay{}, "AliPayAppID")
	// }

	// ma_payment_dx
	// if !db.Migrator().HasIndex(&models.MaPaymentDx{}, "BindID") {
	// 	db.Migrator().CreateIndex(&models.MaPaymentDx{}, "BindID")
	// }
	// if !db.Migrator().HasIndex(&models.MaPaymentDx{}, "JhMerchantNum") {
	// 	db.Migrator().CreateIndex(&models.MaPaymentDx{}, "JhMerchantNum")
	// }

	// ma_payment_jh
	// if !db.Migrator().HasIndex(&models.MaPaymentJh{}, "BindID") {
	// 	db.Migrator().CreateIndex(&models.MaPaymentJh{}, "BindID")
	// }
	// if !db.Migrator().HasIndex(&models.MaPaymentJh{}, "JhMerchantNum") {
	// 	db.Migrator().CreateIndex(&models.MaPaymentJh{}, "JhMerchantNum")
	// }

	// ma_payment_small
	// if !db.Migrator().HasIndex(&models.MaPaymentSmall{}, "BindID") {
	// 	db.Migrator().CreateIndex(&models.MaPaymentSmall{}, "BindID")
	// }
	// if !db.Migrator().HasIndex(&models.MaPaymentSmall{}, "WeChatmchID") {
	// 	db.Migrator().CreateIndex(&models.MaPaymentSmall{}, "WeChatmchID")
	// }
	// if !db.Migrator().HasIndex(&models.MaPaymentSmall{}, "WeChatAppID") {
	// 	db.Migrator().CreateIndex(&models.MaPaymentSmall{}, "WeChatAppID")
	// }

	// ma_payment_wechat
	// if !db.Migrator().HasIndex(&models.MaPaymentWechat{}, "BindID") {
	// 	db.Migrator().CreateIndex(&models.MaPaymentWechat{}, "BindID")
	// }
	// if !db.Migrator().HasIndex(&models.MaPaymentWechat{}, "WeChatmchID") {
	// 	db.Migrator().CreateIndex(&models.MaPaymentWechat{}, "WeChatmchID")
	// }
	// if !db.Migrator().HasIndex(&models.MaPaymentWechat{}, "WeChatAppID") {
	// 	db.Migrator().CreateIndex(&models.MaPaymentWechat{}, "WeChatAppID")
	// }

	// ma_physical_info
	if db.Migrator().HasIndex(&models.MaPhysicalInfo{}, "SalesID") {
		db.Migrator().DropIndex(&models.MaPhysicalInfo{}, "SalesID")
	}
	if db.Migrator().HasIndex(&models.MaPhysicalInfo{}, "AddTime") {
		db.Migrator().DropIndex(&models.MaPhysicalInfo{}, "AddTime")
	}
	if !db.Migrator().HasIndex(&models.MaPhysicalInfo{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaPhysicalInfo{}, "created_at")
	}
	if !db.Migrator().HasIndex(&models.MaPhysicalInfo{}, "idx_addtime_type_age_range_sex") {
		db.Migrator().CreateIndex(&models.MaPhysicalInfo{}, "idx_addtime_type_age_range_sex")
	}
	if !db.Migrator().HasIndex(&models.MaPhysicalInfo{}, "idx_saleid_addtime_msg") {
		db.Migrator().CreateIndex(&models.MaPhysicalInfo{}, "idx_saleid_addtime_msg")
	}

	// ma_robot
	if !db.Migrator().HasIndex(&models.MaRobot{}, "AiFaceMultiple") {
		db.Migrator().CreateIndex(&models.MaRobot{}, "AiFaceMultiple")
	}
	if !db.Migrator().HasIndex(&models.MaRobot{}, "idx_merchantid") {
		db.Migrator().CreateIndex(&models.MaRobot{}, "idx_merchantid")
	}
	if !db.Migrator().HasIndex(&models.MaRobot{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaRobot{}, "created_at")
	}

	// ma_robot_config
	if !db.Migrator().HasIndex(&models.MaRobotConfig{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaRobotConfig{}, "created_at")
	}

	// ma_robot_error_log
	if !db.Migrator().HasIndex(&models.MaRobotErrorLog{}, "SalesID") {
		db.Migrator().CreateIndex(&models.MaRobotErrorLog{}, "SalesID")
	}

	// ma_robot_error_log_pallets
	if !db.Migrator().HasIndex(&models.MaRobotErrorLogPallets{}, "SalesID") {
		db.Migrator().CreateIndex(&models.MaRobotErrorLogPallets{}, "SalesID")
	}

	// ma_robot_inventory
	if !db.Migrator().HasIndex(&models.MaRobotInventory{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaRobotInventory{}, "created_at")
	}

	// ma_robot_report_log
	if !db.Migrator().HasIndex(&models.MaRobotReportLog{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaRobotReportLog{}, "created_at")
	}

	// ma_robot_report_zero_stock_log
	if !db.Migrator().HasIndex(&models.MaRobotReportZeroStockLog{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaRobotReportZeroStockLog{}, "created_at")
	}

	// ma_robot_report_log
	if !db.Migrator().HasIndex(&models.MaRobotReportLog{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaRobotReportLog{}, "created_at")
	}

	// ma_robot_report_zero_stock_log
	if !db.Migrator().HasIndex(&models.MaRobotReportZeroStockLog{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaRobotReportZeroStockLog{}, "created_at")
	}

	// ma_robot_sales_info
	if !db.Migrator().HasIndex(&models.MaRobotSalesInfo{}, "ma_robot_sales_info_ibfk_4") {
		db.Migrator().CreateIndex(&models.MaRobotSalesInfo{}, "ma_robot_sales_info_ibfk_4")
	}
	if !db.Migrator().HasIndex(&models.MaRobotSalesInfo{}, "ma_robot_sales_info_ibfk_5") {
		db.Migrator().CreateIndex(&models.MaRobotSalesInfo{}, "ma_robot_sales_info_ibfk_5")
	}
	if !db.Migrator().HasIndex(&models.MaRobotSalesInfo{}, "SalesID") {
		db.Migrator().CreateIndex(&models.MaRobotSalesInfo{}, "SalesID")
	}
	if !db.Migrator().HasIndex(&models.MaRobotSalesInfo{}, "ContainerStatus") {
		db.Migrator().CreateIndex(&models.MaRobotSalesInfo{}, "ContainerStatus")
	}
	if !db.Migrator().HasIndex(&models.MaRobotSalesInfo{}, "ContainerOne") {
		db.Migrator().CreateIndex(&models.MaRobotSalesInfo{}, "ContainerOne")
	}
	if !db.Migrator().HasIndex(&models.MaRobotSalesInfo{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaRobotSalesInfo{}, "created_at")
	}

	// ma_sales_statement
	if !db.Migrator().HasIndex(&models.MaSalesStatement{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaSalesStatement{}, "created_at")
	}

	// ma_wechat_rate
	if db.Migrator().HasIndex(&models.MaWechatRate{}, "SalesID") {
		db.Migrator().DropIndex(&models.MaWechatRate{}, "SalesID")
	}
	if db.Migrator().HasIndex(&models.MaWechatRate{}, "GoodsID") {
		db.Migrator().DropIndex(&models.MaWechatRate{}, "GoodsID")
	}
	if !db.Migrator().HasIndex(&models.MaWechatRate{}, "idx_SalesID_AddTime") {
		db.Migrator().CreateIndex(&models.MaWechatRate{}, "idx_SalesID_AddTime")
	}
	if !db.Migrator().HasIndex(&models.MaWechatRate{}, "AddTime") {
		db.Migrator().CreateIndex(&models.MaWechatRate{}, "AddTime")
	}
	if !db.Migrator().HasIndex(&models.MaWechatRate{}, "created_at") {
		db.Migrator().CreateIndex(&models.MaWechatRate{}, "created_at")
	}

	return nil
}

func (MigrateFunc) Script33() error {
	mysql.NewDB().Migrator().AutoMigrate(&models.MaDevErrorLog{})
	return nil
}

func (MigrateFunc) Script34() error {
	mysql.NewDB().Migrator().AddColumn(&models.MaGoodsInfo{}, "deduction_limit")
	mysql.NewDB().Migrator().AddColumn(&models.MaOrder{}, "deduction_limit")
	mysql.NewDB().Migrator().AddColumn(&models.MaOrder{}, "deduction_amount")
	mysql.NewDB().Migrator().AddColumn(&models.MaOrder{}, "points_amount")
	mysql.NewDB().Migrator().AddColumn(&models.MaOrder{}, "points_value")
	mysql.NewDB().Migrator().AutoMigrate(&models.MaLotteryMember{})
	mysql.NewDB().Migrator().AutoMigrate(&models.MaLotteryMemberActiveLog{})
	mysql.NewDB().Migrator().AutoMigrate(&models.MaLotteryActivity{})
	mysql.NewDB().Migrator().AutoMigrate(&models.MaLotteryWinners{})
	mysql.NewDB().Migrator().AutoMigrate(&models.MaLotteryPrizeRule{})
	mysql.NewDB().Migrator().AutoMigrate(&models.MaLotteryPrize{})
	mysql.NewDB().Migrator().AutoMigrate(&models.MaLotteryCode{})
	mysql.NewDB().Migrator().AutoMigrate(&models.MaGoodsInfo{})
	return nil
}
